﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Diplom.Controls;
using Diplom.NumericArrays;
using Diplom.Utilities;
using Diplom.Graphics.D2D;

namespace Diplom.Forms
{
    public partial class ArrayActionDesigner : Form
    {
        ArrayInfo arrayInfo = null;
        Numeric1DArray array1D = null;

        string m_interval = "";
        string m_length = "";

        public ArrayActionDesigner(string interval, string length)
        {
            InitializeComponent();

            this.m_interval = interval;
            this.m_length = length;
        }

        private void ArrayActionDesigner_Load(object sender, EventArgs e)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture =
                System.Globalization.CultureInfo.InvariantCulture;

            arrayInfo = new ArrayInfo();
            arrayInfo.Location = new Point(0, 0);

            arrayInfo.txtBoxInterval.Text = m_interval;
            arrayInfo.txtBoxLength.Text = m_length;

            arrayInfo.buttonAction.Enabled = false;
            arrayInfo.buttonActionRemove.Enabled = false;
            arrayInfo.buttonDeleteArray.Enabled = false;            
            arrayInfo.selectAction.Enabled = false;
            arrayInfo.txtBoxAmplitude.TextChanged += new EventHandler(ReCalculateData);
            arrayInfo.txtBoxFrequence.TextChanged += new EventHandler(ReCalculateData);
            //arrayInfo.txtBoxInterval.TextChanged += new EventHandler(ReCalculateData);
            //arrayInfo.txtBoxInterval.Enabled = false;
            //arrayInfo.txtBoxLength.TextChanged += new EventHandler(ReCalculateData);
            arrayInfo.txtBoxLength.Enabled = false;
            arrayInfo.txtBoxPhase.TextChanged += new EventHandler(ReCalculateData);
            arrayInfo.comboBoxSelFunc.SelectedIndexChanged += new EventHandler(ReCalculateData);

            

            this.Controls.Add(arrayInfo);

            array1D = new Numeric1DArray();
            Function function = Function.Cos;
            int length = Int32.Parse(arrayInfo.txtBoxLength.Text);
            double frequency = double.Parse(arrayInfo.txtBoxFrequence.Text);
            string[] interval = arrayInfo.txtBoxInterval.Text.Split(new String[] { " ", ";" }, StringSplitOptions.RemoveEmptyEntries);
            double phase = double.Parse(arrayInfo.txtBoxPhase.Text);
            double begin = double.Parse(interval[0]);
            double end = double.Parse(interval[1]);
            double amplitude = double.Parse(arrayInfo.txtBoxAmplitude.Text);
            Parameters parameters = new Parameters(length, frequency, phase, amplitude, begin, end);
            ArrayGenerator.Generate(function,
                ref array1D, parameters);
        }

        private void ReCalculateData(object sender, EventArgs e)
        {
            array1D = new Numeric1DArray();

            Function function;
            switch (arrayInfo.comboBoxSelFunc.Text)
            {
                case "Noise":
                    {
                        function = Function.Noise;
                    } break;
                case "Sin":
                    {
                        function = Function.Sin;
                    } break;
                case "Cos":
                    {
                        function = Function.Cos;
                    } break;
                case "CorrelationNoise":
                    {
                        function = Function.CorrelationNoise;
                    } break;
                case "Exp":
                    {
                        function = Function.Exp;
                    } break;
                case "Dirac":
                    {
                        function = Function.Dirac;
                    } break;
                case "Heaviside":
                    {
                        function = Function.Heaviside;
                    } break;
                default:
                    {
                        errorLabel.Text = "Unknown function";
                        return;
                    }

            }
            int length;
            try
            {
                length = Int32.Parse(arrayInfo.txtBoxLength.Text);
            }
            catch
            {
                errorLabel.Text = "Error in parsing Length field";
                return;
            }

            double frequency;
            try
            {
                frequency = double.Parse(arrayInfo.txtBoxFrequence.Text);
            }
            catch
            {
                errorLabel.Text = "Error in parsing Frequency field";
                return;
            }

            double phase;
            try
            {
                phase = double.Parse(arrayInfo.txtBoxPhase.Text);
            }
            catch
            {
                errorLabel.Text = "Error in parsing Phase field";
                return;
            }

            string[] interval = arrayInfo.txtBoxInterval.Text.Split(new String[] { " ", ";" }, StringSplitOptions.RemoveEmptyEntries);
            double begin;
            double end;
            try
            {
                begin = double.Parse(interval[0]);
                end = double.Parse(interval[1]);
            }
            catch
            {
                errorLabel.Text = "Error in parsing Interval field";
                return;
            }

            double amplitude;
            try
            {
                amplitude = double.Parse(arrayInfo.txtBoxAmplitude.Text);
                if (amplitude < 1e-6) throw new Exception();
            }
            catch
            {
                errorLabel.Text = "Error in parsing Amplitude field";
                return;
            }

            Parameters parameters = new Parameters(length, frequency, phase, amplitude, begin, end);
            ArrayGenerator.Generate(function,
                ref array1D, parameters);

            errorLabel.Text = "";
            this.Refresh();
        }

        private void ArrayActionDesigner_Paint(object sender, PaintEventArgs e)
        {
            pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);

            if (arrayInfo != null)
            {
                Image img = pictureBox1.Image;
                System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(img);
                DrawStyle style = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Red);
                
                D2DVisualizer.DrawGraphic(ref gr,
                    ref array1D,
                    new RectangleF(0, 0,
                        pictureBox1.Width, pictureBox1.Height), style);


                pictureBox1.Image = img;
            }
        }

        public Numeric1DArray GetArray()
        {
            return array1D;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            switch (arrayInfo.comboBoxSelFunc.Text)
            {
                case "Noise":
                    {
                    } break;
                case "Sin":
                    {
                    } break;
                case "Cos":
                    {
                    } break;
                case "CorrelationNoise":
                    {
                    } break;
                case "Exp":
                    {
                    } break;
                case "Dirac":
                    {
                    } break;
                case "Heaviside":
                    {
                    } break;
                default:
                    {
                        errorLabel.Text = "Unknown function";
                        this.DialogResult = DialogResult.None;
                        return;
                    }

            }
            try
            {
                Int32.Parse(arrayInfo.txtBoxLength.Text);
            }
            catch
            {
                errorLabel.Text = "Error in parsing Length field";
                this.DialogResult = DialogResult.None;
                return;
            }

            try
            {
                double.Parse(arrayInfo.txtBoxFrequence.Text);
            }
            catch
            {
                errorLabel.Text = "Error in parsing Frequency field";
                this.DialogResult = DialogResult.None;
                return;
            }
            
            try
            {
                double.Parse(arrayInfo.txtBoxPhase.Text);
            }
            catch
            {
                errorLabel.Text = "Error in parsing Phase field";
                this.DialogResult = DialogResult.None;
                return;
            }

            string[] interval = arrayInfo.txtBoxInterval.Text.Split(new String[] { " ", ";" }, StringSplitOptions.RemoveEmptyEntries);
            try
            {
                double.Parse(interval[0]);
                double.Parse(interval[1]);
            }
            catch
            {
                errorLabel.Text = "Error in parsing Interval field";
                this.DialogResult = DialogResult.None;
                return;
            }

            double amplitude;
            try
            {
                amplitude = double.Parse(arrayInfo.txtBoxAmplitude.Text);
                if (amplitude < 1e-6) throw new Exception();
            }
            catch
            {
                errorLabel.Text = "Error in parsing Amplitude field";
                this.DialogResult = DialogResult.None;
                return;
            }
        }
    }
}
